import Sequelize from 'sequelize';
import express from 'express';
import { statusList, sequelizeStatus } from '../../common/status'; // 状态码对照
import { User } from '../../modal/index'; // 数据库实体类
import { checkPermission } from '../../utils/utils'; // 工具类

const router = express.Router();
const permission = ["all"];

sequelizeStatus.SequelizeUniqueConstraintError = "对不起，用户名已存在" // 重写错误代码

router.post('/', async function (Request, Response, next) {
  //权限校验
  await checkPermission(Request, Response, permission)
  let uid = JSON.parse(Request.signedCookies.token).uid
  let param = Request.body;
  let updateParam = {}
  if (param.username) {
    updateParam.username = param.username;
  }
  if (param.mobile) {
    updateParam.mobile = param.mobile;
  }
  if (param.email) {
    updateParam.email = param.email;
  }
  console.log(updateParam)
  User.Modal.update({
    ...updateParam
  }, {
      where: {
        id: uid,
      }
    }).then(res => {
      if (res && res[0] > 0) {
        Response.json({
          data: "",
          message: "success"
        });
      } else {
        Response.status(300).send({ message: "修改用户信息查询失败" });
      }
    }).catch(err => {
      console.log(err)
      Response.status(500).send({ message: sequelizeStatus[err.name] || statusList[500] || err.name });
    });
});

module.exports = router;